public class Link {
    class Lian
    {
        int val;
        Lian next;

        public Lian(int val) {
            this.val = val;
        }
    }
    public Lian head1;
    public Lian head2;
    public Lian build1()
    {
        Lian a=new Lian(1);
        Lian b=new Lian(2);
        Lian c=new Lian(3);
        Lian d=new Lian(6);
        Lian e=new Lian(7);

        head1=a;
        a.next=b;
        b.next=c;
        c.next=d;
        d.next=e;
        return head1;

    }
    public Lian build2()
    {
        Lian a=new Lian(4);
        Lian b=new Lian(5);
        Lian c=new Lian(6);
        Lian d=new Lian(7);
        head2=a;
        a.next=b;
        b.next=c;
        c.next=d;
        return head2;


    }
    public Lian FindFirstCommonNode(Lian pHead1, Lian pHead2) {
        if(pHead1==null||pHead2==null)
        {
            return null;
        }
        Lian cur1=pHead1;
        Lian cur2=pHead2;
        int count1=0;
        int count2=0;
        while(cur1!=null)
        {
            count1++;
            cur1=cur1.next;
        }
        while(cur2!=null)
        {
            count2++;
            cur2=cur2.next;
        }
        cur1=pHead1;
        cur2=pHead2;

        if(count1>count2)
        {
            int a=count1-count2;
            while(a!=0)
            {
                cur1=cur1.next;
                a--;
            }
        }
        else
        {
            int c=count2-count1;
            while(c!=0)
            {
                cur2=cur2.next;
                c--;
            }
        }
        while(cur1!=null)
        {
            if(cur1==cur2)
            {
                return cur1;
            }
        }
        return null;


















































    }




















































    public Lian addInList (Lian head1, Lian head2) {
       Lian c=reverse(head1);
       Lian d=reverse(head2);
       Lian cur1=c;
       Lian cur2=d;
        int carry=0;


        Lian tmp=new Lian(-1);
        Lian pre=tmp;
        int sum=0;

        while(cur1!=null||cur2!=null)
        {
            sum=carry;



            if(cur1!=null)
            {
               sum+=cur1.val;
               cur1=cur1.next;
            }
            if(cur2!=null)
            {
                sum+=cur2.val;
                cur2=cur2.next;
            }




          Lian z=new Lian(sum%10);
            carry=sum/10;
            pre.next=z;
            pre=pre.next;
            //cur1=cur1.next;
            //cur2=cur2.next;






        }
        if(carry!=0)
        {
            Lian z=new Lian(carry);
            pre.next=z;
            pre=pre.next;
        }
        tmp=tmp.next;
        Lian m=reverse(tmp);
        return m;








        // write code here
    }
    public Lian reverse(Lian tmp)
    {
        if(tmp==null)
        {
            return null;
        }
        if(tmp.next==null)
        {
            return tmp;
        }
        Lian pre=null;
        Lian cur=tmp;
        while(cur.next!=null)
        {
           Lian c=cur.next;
            cur.next=pre;
            pre=cur;
            cur=c;
        }
        cur.next=pre;
        return cur;
    }


}
class text1{
    public static void main(String[] args) {
       Link a=new Link();

       a.addInList(a.build1(),a.build2());
       a.FindFirstCommonNode(a.build1(), a.build2());



    }
}

